import java.util.*;

public class Solution {

    //
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        long[] sums = new long[n + 1];
        for(int i = 1; i <= n; i++){
            sums[i] = sums[i - 1] + in.nextLong();
        }

        while(m-- != 0){
            int l = in.nextInt(), r = in.nextInt();
            System.out.println(sums[r] - sums[l - 1]);
        }
    }

    //
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
        int[][] arr = new int[n][m];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                arr[i][j] = in.nextInt();
            }
        }

        long[][] sums = new long[n + 1][m + 1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                sums[i][j] = sums[i - 1][j] + sums[i][j - 1] - sums[i - 1][j - 1] + arr[i - 1][j - 1];
            }
        }

        while(q-- != 0){
            int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt();
            long ret = sums[x2][y2] - sums[x1 - 1][y2] - sums[x2][y1 - 1] + sums[x1 - 1][y1 - 1];
            System.out.println(ret);
        }
    }

    //
    public int pivotIndex(int[] nums) {
        int n = nums.length, sum = nums[0];
        int[] prev = new int[n];
        for(int i = 1; i < n; i++){
            prev[i] = prev[i - 1] + nums[i - 1];
            sum += nums[i];
        }

        for(int i = 0; i < n; i++){
            int left = prev[i], right = sum - nums[i] - prev[i];
            if(left == right) { return i;}
        }
        return -1;
    }

    //
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = g[n - 1] = 1;
        for(int i = 1; i < n; i++){
            f[i] = f[i - 1] * nums[i - 1];
        }
        for(int i = n - 2; i >= 0; i--){
            g[i] = g[i + 1] * nums[i + 1];
        }

        int[] ans = new int[n];
        for(int i = 0; i < n; i++){
            ans[i] = f[i] * g[i];
        }
        return ans;
    }

    //
    public int subarraySum(int[] nums, int k) {
        int n = nums.length;
        int[] prev = new int[n + 1];
        for(int i = 1; i <= n; i++){
            prev[i] = prev[i - 1] + nums[i - 1];
        }

        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = i; j <= n; j++){
                if(prev[j] - prev[i - 1] == k) { ans++;}
            }
        }
        return ans;
    }

    public int subarraySum2(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, 1);

        int sum = 0, ans = 0;
        for(int x : nums){
            sum += x;
            ans += hash.getOrDefault(sum - k, 0);
            hash.put(sum, hash.getOrDefault(sum, 0) + 1);
        }
        return ans;
    }

    //
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0 % k, 1);

        int sum = 0, ans = 0;
        for(int x : nums){
            sum += x;
            int r = (sum % k + k) % k;
            ans += hash.getOrDefault(r, 0);
            hash.put(r, hash.getOrDefault(r, 0) + 1);
        }
        return ans;
    }

    //
    public int findMaxLength(int[] nums) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, -1);

        int sum = 0, ret = 0;
        for(int i = 0; i < nums.length; i++){
            sum += nums[i] == 0 ? -1 : 1;
            if(hash.containsKey(sum)){
                ret = Math.max(ret, i - hash.get(sum));
            }else{
                hash.put(sum, i);
            }
        }
        return ret;
    }

    //
    public int[][] matrixBlockSum(int[][] mat, int k) {
        int m = mat.length, n = mat[0].length;

        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
            }
        }

        int[][] ans = new int[m][n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                int x1 = Math.max(i - k, 0) + 1;
                int y1 = Math.max(j - k, 0) + 1;
                int x2 = Math.min(i + k, m - 1) + 1;
                int y2 = Math.min(j + k, n - 1) + 1;
                ans[i][j] = dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1];
            }
        }
        return ans;
    }

    //
    public static void main3(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        double ret = 1.0;
        for(int i = n; i >= n - m + 1; i--) { ret *= i;}
        for(int i = m; i >= 2; i--) { ret /= i;}
        for(int i = 0; i < m; i++) { ret *= 0.8;}
        for(int i = 0; i < n - m; i++) { ret *= 0.2;}

        System.out.printf("%.4f", ret);
    }

    //
    public static int n, l, r;
    public static int[] arr;

    //差值在 [0, x] 区间内一共有多少对
    public static long find(int x){
        int left = 0, right = 0;
        long ret = 0;
        while(right < n){
            while(arr[right] - arr[left] > x) { left++;}
            ret += right - left;
            right++;
        }
        return ret;
    }

    public static void main4(String[] args){
        Scanner in = new Scanner(System.in);
        n = in.nextInt(); l = in.nextInt(); r = in.nextInt();
        arr = new int[n];
        for(int i = 0; i < n; i++) { arr[i] = in.nextInt();}
        Arrays.sort(arr);

        System.out.println(find(r) - find(l - 1));
    }

    //
    public static void main5(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int x = 1, y = 2;
        int MOD = 1000000007;

        for(int i = 2; i <= n; i++){
            int xx = x, yy = y;
            x = (2 * yy + 1) % MOD;
            y = ((2 * yy + 2) % MOD + xx) % MOD;
        }
        System.out.println(x + " " + y);
    }

    //
}
